﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using PeachLite.Entity.Admin;

namespace PeachLite.Admin
{
    /// <summary>
    /// 页面基类
    /// </summary>
    public abstract class PageBase : Page
    {
        #region 常量

        private const string SESSION_ADMIN = "SESSION_ADMIN";

        #endregion

        #region 属性

        /// <summary>
        /// 管理员信息
        /// </summary>
        protected AdminAccount Admin
        {
            get
            {
                if (Session[SESSION_ADMIN] == null)
                {
                    Session[SESSION_ADMIN] = new AdminAccount();
                }
                return (AdminAccount)Session[SESSION_ADMIN];
            }
            set
            {
                Session[SESSION_ADMIN] = (AdminAccount)value;
            }
        }

        #endregion

        #region 方法

        protected virtual void Do()
        {
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Admin.IsLogin)
            {
                Do();
            }
            else
            {
               Response.Redirect("login.aspx");
               // Page.ClientScript.RegisterStartupScript(this.GetType(), "msgbox", "<script type=\"text/javascript\">top.location.href ='login.aspx';</script>");
            }
        }

        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="flag">标志</param>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="message">url</param>
        protected void ShowMessage(int flag, string title, string message, string url)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "msgbox", string.Format("<script type=\"text/javascript\">alert('{0}');{1}</script>", message, string.IsNullOrWhiteSpace(url) ? "" : "self.location='" + url + "';"));
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msgbox", string.Format("<script type=\"text/javascript\">showmsg('{0}','{1}','{2}','{3}');</script>", flag, title, message, url));
        }

        #endregion
    }
}